package com.cn.kehong.controller;


import com.alibaba.fastjson.JSONObject;

import com.cn.kehong.api.IWxUserService;
import com.cn.kehong.domain.WxUser;
import com.cn.kehong.utils.CommonUtil;
import com.cn.kehong.utils.JsonResponse;
import com.cn.kehong.utils.MD5Util;
import com.cn.kehong.utils.QueryForm;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/wxUser")
public class WxUserController {

    @Autowired
    private IWxUserService wxUserService;


    @ResponseBody
    @RequestMapping("/queryWxUserList")
    public JsonResponse<List<WxUser>> queryWxUserList(QueryForm queryForm) throws Exception {
        JsonResponse<List<WxUser>> response = new JsonResponse<List<WxUser>>();
        List<WxUser> loginResult = wxUserService.queryWxUserList(queryForm);
        response.setResult(loginResult);
        response.setSuccess(true);
        return response;
    }

    @ResponseBody
    @RequestMapping("/queryWxUserById")
    public JsonResponse<WxUser> queryWxUserById(String userId) throws Exception {
        JsonResponse<WxUser> response = new JsonResponse<WxUser>();
        WxUser loginResult = wxUserService.queryWxUserById(userId);
        response.setResult(loginResult);
        response.setSuccess(true);
        return response;
    }


    @ResponseBody
    @RequestMapping("/queryWxUserSelectList")
    public JsonResponse<List<WxUser>> queryLoginSelectList() throws Exception {
        JsonResponse<List<WxUser>> response = new JsonResponse<List<WxUser>>();
        List<WxUser> loginResult = wxUserService.queryWxUserSelectList();
        response.setResult(loginResult);
        response.setSuccess(true);
        return response;
    }

    @ResponseBody
    @RequestMapping("/addWxUser")
    public JsonResponse<String> addWxUser(WxUser login) throws Exception {
        JsonResponse<String> response = new JsonResponse<String>();
        String result = wxUserService.addWxUser(login);
        response.setResult(result);
        response.setSuccess(true);
        return response;
    }

    @ResponseBody
    @RequestMapping("/updateWxUser")
    public JsonResponse<String> updateWxUser(WxUser login) throws Exception {
        JsonResponse<String> response = new JsonResponse<String>();
        String result = wxUserService.updateWxUser(login);
        response.setResult(result);
        response.setSuccess(true);
        return response;
    }

    @ResponseBody
    @RequestMapping("/updateWxUserPassword")
    public JsonResponse<String> updateWxUserPassword(WxUser login) throws Exception {
        JsonResponse<String> response = new JsonResponse<String>();
        String result = wxUserService.updateWxUserPassword(login);
        return CommonUtil.getResponseData(response,result);
    }

    @ResponseBody
    @RequestMapping("/updateWxUserStatus")
    public JsonResponse<String> updateWxUserStatus(WxUser login) throws Exception {
        JsonResponse<String> response = new JsonResponse<String>();
        String result = wxUserService.updateWxUserStatus(login);
        response.setResult(result);
        response.setSuccess(true);
        return response;
    }


    @ResponseBody
    @RequestMapping("/delWxUser")
    public JsonResponse<String> delWxUser(@RequestBody QueryForm queryForm) throws Exception {
        JsonResponse<String> response = new JsonResponse<String>();
        String result = wxUserService.delWxUser(queryForm);
        response.setResult(result);
        response.setSuccess(true);
        return response;
    }

    @ResponseBody
    @RequestMapping("/sendCode")
    public JsonResponse<String> sendCode(@RequestParam(value = "phone", required = true) String phone) throws Exception {
        JsonResponse<String> response = new JsonResponse<String>();
        String result = wxUserService.sendCode(phone);
        response.setResult(result);
        response.setSuccess(true);
        return response;
    }


    @ResponseBody
    @RequestMapping("/doLogin")
    public JsonResponse<WxUser> doLogin(WxUser login) throws Exception {
        JsonResponse<WxUser> response = new JsonResponse<WxUser>();
        if (StringUtils.isNotBlank(login.getWxPassword())) {
            login.setWxPassword(MD5Util.MD5(login.getWxPassword()));
        }
        WxUser result = wxUserService.doLogin(login);
        response.setSuccess(null == result ? false : true);
        response.setResult(result);
        return response;
    }

    @ResponseBody
    @RequestMapping("/api/getPhoneNum")
    public JsonResponse<JSONObject> updateOrGetPhoneNum(String  code,String openId) throws Exception {
        JsonResponse<JSONObject> response = new JsonResponse<JSONObject>();
        JSONObject result = wxUserService.updateOrGetPhoneNum(code,openId);
        response.setResult(result);
        response.setSuccess(true);
        return response;
    }
    @ResponseBody
    @RequestMapping("/api/getWxUserDetail")
    public JsonResponse<JSONObject> getWxUserDetail(String  userId) throws Exception {
        JsonResponse<JSONObject> response = new JsonResponse<JSONObject>();
        JSONObject result = wxUserService.getWxUserDetail(userId);
        response.setResult(result);
        response.setSuccess(true);
        return response;
    }

    @ResponseBody
    @RequestMapping("/api/getWxOpenId")
    public JsonResponse<String> getWxOpenId(String  code) throws Exception {
        JsonResponse<String> response = new JsonResponse<String>();
        String result = wxUserService.getWxOpenId(code);
        response.setResult(result);
        response.setSuccess(true);
        return response;
    }


    @ResponseBody
    @RequestMapping("/api/updateWxUserInfo")
    public JsonResponse<String> updateWxUserInfo(WxUser wxUser) throws Exception {
        JsonResponse<String> response = new JsonResponse<String>();
        String result = wxUserService.updateWxUserInfo(wxUser);
        return CommonUtil.getResponseData(response,result);
    }

    @ResponseBody
    @RequestMapping("/api/updateWxUserInfoByOpenId")
    public JsonResponse<WxUser> updateWxUserInfoByOpenId(@RequestBody  WxUser wxUser) throws Exception {
        JsonResponse<WxUser> response = new JsonResponse<WxUser>();
        WxUser loginResult = wxUserService.updateWxUserImgById(wxUser);
        response.setResult(loginResult);
        response.setSuccess(loginResult == null ?false:true);
        response.setMessage(loginResult == null ?"更新头像失败！":"");
        return response;
    }


}
